<template>
<div class="login-container">

  <div class="login-main">
    <div class="login-title-dialog fl">
      <div class="title-icon">
       <i class="fa fa-address-card-o" aria-hidden="true"></i>
      </div>
      <div class="title-txt">
        <h2>马鞍山籍人才信息库系统</h2>
      </div>
    </div>
    <div class="fr">
      <el-form autoComplete="on" :model="loginForm" :rules="loginRules" ref="loginForm" label-position="left" label-width="0px" class="card-box login-form">
        <el-tabs v-model="activeName" @tab-click="handleClick">
          <el-tab-pane label="账号密码登录" name="first">
                <el-form-item prop="username">
                  <el-input name="username" type="text" v-model="loginForm.username" autoComplete="on" placeholder="账户">
                      <i slot="prefix" class="el-input__icon el-icon-search"></i>
                  </el-input>
                </el-form-item>
                <el-form-item prop="password">
                  <el-input name="password" type="password" @keyup.enter.native="handleLogin" v-model="loginForm.password" autoComplete="on" placeholder="密码"></el-input>
                </el-form-item>
            </el-tab-pane>
          <el-tab-pane label="手机号登录" name="second">
                <el-form-item prop="username">
                  <el-input name="username" type="text" prefix-icon="el-icon-mobile-phone" v-model="loginForm.username" autoComplete="on" placeholder="手机号"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                  
                  <el-row :gutter="10">
                    <el-col :span="15">
                      <el-input name="password" type="password" prefix-icon="el-icon-message" @keyup.enter.native="handleLogin" v-model="loginForm.password" autoComplete="on" placeholder="验证码"></el-input>
                    </el-col>
                    <el-col :span="9" class="text-right">
                      <el-button>获取验证码</el-button>
                    </el-col>
                  </el-row>
                </el-form-item>
            </el-tab-pane>
        </el-tabs>
        <el-form-item>
          <el-button type="primary" style="width:100%;" :loading="loading" @click.native.prevent="handleLogin">
            登录
          </el-button>
        </el-form-item>
        <div class="login-registry-row text-right">
          <a href="#/registry"><span>注册用户</span></a>
        </div>
      </el-form>
    </div>
    <div class="clearfix"></div>
  </div>

  <el-dialog title="第三方验证" :visible.sync="showDialog">
    邮箱登录成功,请选择第三方验证
    <social-sign></social-sign>
  </el-dialog>

</div>
</template>

<script>
import socialSign from './socialsignin';

export default {
  components: {
    socialSign
  },
  name: 'login',
  data() {
    const validatePass = (rule, value, callback) => {
      if (value.length < 5) {
        callback(new Error('密码不能小于5位'));
      } else {
        callback();
      }
    };
    return {
      activeName: 'second',
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{
          required: true,
          message: '账户不能为空',
          trigger: 'blur'
        },
        {
          min: 3,
          max: 20,
          message: '长度在 3 到 20 个字符',
          trigger: 'blur'
        }],
        password: [{
          required: true,
          trigger: 'blur',
          validator: validatePass
        }]
      },
      loading: false,
      showDialog: false
    }
  },
  methods: {
    handleClick(tab, event) {
      console.log(tab, event);
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          this.$store.dispatch('LoginByEmail', this.loginForm).then(() => {
            this.loading = false;
            this.$router.push({
              path: '/'
            });
            // this.showDialog = true;
          }).catch(() => {
            this.loading = false;
          });
          // this.$http.post('/oauth/token', {
          //   username: this.loginForm.username,
          //   password: this.loginForm.password
          // }, {
          //   headers: {
          //     Authorization: 'Basic enV1bDp6dXVs'
          //   },
          //   emulateJSON: true,
          //   emulateHTTP: true
          // }).then(function(response) {
          //   if (response.status === 200) {
          //     this.$store.dispatch('LoginByEmail', this.loginForm).then(() => {
          //       this.loading = false;
          //       this.$router.push({
          //         path: '/'
          //       });
          //       // this.showDialog = true;
          //     }).catch(() => {
          //       this.loading = false;
          //     });
          //   } else {
          //     this.loading = false;
          //     return false;
          //   }
          // }, function(error) {
          //   console.log('server error!!');
          //   this.loading = false;
          //   return false;
          // });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    afterQRScan() {
      // const hash = window.location.hash.slice(1);
      // const hashObj = getQueryObject(hash);
      // const originUrl = window.location.origin;
      // history.replaceState({}, '', originUrl);
      // const codeMap = {
      //   wechat: 'code',
      //   tencent: 'code'
      // };
      // const codeName = hashObj[codeMap[this.auth_type]];
      // if (!codeName) {
      //   alert('第三方登录失败');
      // } else {
      //   this.$store.dispatch('LoginByThirdparty', codeName).then(() => {
      //     this.$router.push({ path: '/' });
      //   });
      // }
    }
  },
  created() {
    // window.addEventListener('hashchange', this.afterQRScan);
  },
  destroyed() {
    // window.removeEventListener('hashchange', this.afterQRScan);
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
@import "src/styles/mixin.scss";
.tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 5px;
}
.login-main{
    position: absolute;
    top: 50%;
    left: 50%;
    margin-left: -420px;
    margin-top: -200px;
}
/*登陆框样式*/
.login-title-dialog{
    // width: 400px;
    padding: 100px 80px 15px;
    text-align: center;
    .title-icon{
      color: #8398ac;
      i{
        font-size: 48px;
      }
    }
    .title-txt{
      color: #8398ac;
    }
}
.text-right{
  text-align: right;
}

.login-container {
    background-image: url('../../assets/svg/login-bg.svg');
    background-repeat: no-repeat;
    background-position: center 110px;
    background-size: 100%;

    @include relative;
    height: 100vh;
    background-color: #f0f2f5;
    // input:-webkit-autofill {
    //     -webkit-box-shadow: 0 0 0 1000px #293444 inset !important;
    //     -webkit-text-fill-color: #fff !important;
    // }
    // input {
    //     background: transparent;
    //     border: 0;
    //     -webkit-appearance: none;
    //     border-radius: 0;
    //     padding: 12px 5px 12px 15px;
    //     color: #eeeeee;
    //     height: 47px;
    // }
    // .el-input {
    //     display: inline-block;
    //     height: 47px;
    //     width: 85%;
    // }
    .svg-container {
        padding: 6px 5px 6px 15px;
        color: #889aa4;
    }
    .title {
        font-size: 26px;
        font-weight: 400;
        color: #eeeeee;
        margin: 0 auto 40px;
        text-align: center;
        font-weight: bold;
    }
    .login-form {
        // position: absolute;
        // left: 0;
        // right: 0;
        // width: 400px;
        padding: 35px 35px 15px;
        // margin: 120px auto;
    }
    .el-form-item {
        border: 1px solid rgba(255, 255, 255, 0.1);
        // background: rgba(0, 0, 0, 0.1);
        border-radius: 5px;
        color: #454545;
    }
    .forget-pwd {
        color: #fff;
    }
    .login-registry-row{
      a{
        color: #1890ff;
      }
    }
}
</style>
